package com.sc.testservice;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.sc.test.IAidlInterface;
import com.sc.testservice.service.IService;
import com.sc.testservice.service.MyService;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    private IService myBinder;
    private MyConn myConn;
    private IAidlInterface aidlInterface;
    private MyConn2 myConn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

    }

    @OnClick({R.id.btn_start_service, R.id.btn_stop_service, R.id.btn_change_service,
            R.id.btn_bind_service, R.id.btn_unbind_service, R.id.btn_bind_remote_service,
            R.id.btn_unbind_remote_service, R.id.btn_change_remote_service})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.btn_start_service:
                intent = new Intent(this, MyService.class);
                startService(intent);
                break;
            case R.id.btn_stop_service:
                intent = new Intent(this, MyService.class);
                stopService(intent);
                break;
            case R.id.btn_change_service:
                myBinder.callChange("aaaa");
                break;
            case R.id.btn_bind_service:
                intent = new Intent(this, MyService.class);
                myConn = new MyConn();
                bindService(intent, myConn, 0);
                break;
            case R.id.btn_unbind_service:
                unbindService(myConn);
                break;
            case R.id.btn_bind_remote_service:
                intent = new Intent();
                intent.setAction("com.sc.test");
                //android 5.0以后必须指定包名 否则异常
                intent.setPackage("com.sc.test");
                myConn2 = new MyConn2();
                bindService(intent, myConn2, Service.BIND_AUTO_CREATE);
                break;
            case R.id.btn_unbind_remote_service:
                unbindService(myConn2);
                break;
            case R.id.btn_change_remote_service:
                try {
                    aidlInterface.change("hahahahaha");
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    class MyConn implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d("activity", "服务返回");
            myBinder = (IService) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }

    class MyConn2 implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            aidlInterface = IAidlInterface.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
}
